﻿@page "/master-detail-hierarchy"

@using RadzenBlazorDemos.Data
@using RadzenBlazorDemos.Models.Northwind
@using Microsoft.EntityFrameworkCore

@inject NorthwindContext dbContext

<h1>DataGrid Master Detail Hierarchy</h1>

<p>This page demonstrates how to use templates to create a Radzen Blazor DataGrid hierarchy.</p>

<RadzenExample Name="MasterDetailHierarchy" Heading="false" Documentation="false">
    <RadzenDataGrid @ref="grid" AllowFiltering="true" AllowPaging="true" PageSize="3" AllowSorting="true" RowRender="@RowRender" ExpandMode="DataGridExpandMode.Single"
                Data="@orders" TItem="Order">
        <Template Context="order">
            <RadzenCard Style="margin-bottom:20px">
                Company:
                <b>@order.Customer?.CompanyName</b>
            </RadzenCard>
            <RadzenTabs>
                <Tabs>
                    <RadzenTabsItem Text="Order Details">
                        <RadzenDataGrid AllowFiltering="true" AllowPaging="true" AllowSorting="true" Data="@order.OrderDetails" TItem="OrderDetail">
                            <Columns>
                                <RadzenDataGridColumn TItem="OrderDetail" Property="Order.CustomerID" Title="Order" />
                                <RadzenDataGridColumn TItem="OrderDetail" Property="Product.ProductName" Title="Product" />
                                <RadzenDataGridColumn TItem="OrderDetail" Property="UnitPrice" Title="Unit Price">
                                    <Template Context="detail">
                                        @String.Format(new System.Globalization.CultureInfo("en-US"), "{0:C}", detail.UnitPrice)
                                    </Template>
                                </RadzenDataGridColumn>
                                <RadzenDataGridColumn TItem="OrderDetail" Property="Quantity" Title="Quantity" />
                                <RadzenDataGridColumn TItem="OrderDetail" Property="Discount" Title="Discount">
                                    <Template Context="detail">
                                        @String.Format("{0}%", detail.Discount * 100)
                                    </Template>
                                </RadzenDataGridColumn>
                            </Columns>
                        </RadzenDataGrid>
                    </RadzenTabsItem>
                    <RadzenTabsItem Text="Products">
                        <RadzenDataList WrapItems="true" AllowPaging="true" Data="@order.OrderDetails" TItem="OrderDetail" PageSize="10">
                            <Template Context="detail">
                                <RadzenCard Style="width:100px;height:100px">
                                    Product:
                                    <b>@detail?.Product?.ProductName</b>
                                </RadzenCard>
                            </Template>
                        </RadzenDataList>
                    </RadzenTabsItem>
                </Tabs>
            </RadzenTabs>
        </Template>
        <Columns>
            <RadzenDataGridColumn Width="100px" TItem="Order" Property="OrderID" Title="Order ID" />
            <RadzenDataGridColumn Width="200px" TItem="Order" Property="Customer.CompanyName" Title="Customer" />
            <RadzenDataGridColumn Width="150px" TItem="Order" Property="Employee.LastName" Title="Employee">
                <Template Context="order">
                    <RadzenImage Path="@order.Employee?.Photo" style="width: 40px; height: 40px; border-radius: 8px;" />
                    @order.Employee?.FirstName @order.Employee?.LastName
                </Template>
            </RadzenDataGridColumn>
            <RadzenDataGridColumn TItem="Order" Property="OrderDate" Title="Order Date" FormatString="{0:d}" />
            <RadzenDataGridColumn TItem="Order" Property="RequiredDate" Title="Required Date" FormatString="{0:d}" />
            <RadzenDataGridColumn TItem="Order" Property="ShippedDate" Title="Shipped Date" FormatString="{0:d}" />
            <RadzenDataGridColumn TItem="Order" Property="ShipName" Title="Ship Name" />
            <RadzenDataGridColumn TItem="Order" Property="ShipCountry" Title="Ship Country" />
        </Columns>
    </RadzenDataGrid>
</RadzenExample>
@code {
    IEnumerable<Order> orders;
    RadzenDataGrid<Order> grid;

    protected override void OnInitialized()
    {
        orders = dbContext.Orders.Include("Customer").Include("Employee").Include("OrderDetails").Include("OrderDetails.Product").ToList();
    }

    void RowRender(RowRenderEventArgs<Order> args)
    {
        args.Expandable = args.Data.ShipCountry == "France" || args.Data.ShipCountry == "Brazil";
    }

    protected override void OnAfterRender(bool firstRender)
    {
        if (firstRender)
        {
            grid.ExpandRow(orders.FirstOrDefault());
            StateHasChanged();
        }

        base.OnAfterRender(firstRender);
    }
}